package com.gen.springbootserver.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.gen.springbootserver.mybatis.dao.RoleMapper;
import com.gen.springbootserver.mybatis.dao.UserRolesMapper;
import com.gen.springbootserver.mybatis.model.Role;
import com.gen.springbootserver.mybatis.model.RoleExample;
import com.gen.springbootserver.mybatis.model.UserRoles;
import com.gen.springbootserver.mybatis.model.UserRolesExample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleService {

    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserRolesMapper userRolesMapper;

    public Set<Role> getUserRoleByUserId(Long userId) {
        UserRolesExample userRolesExample = new UserRolesExample();
        userRolesExample.createCriteria().andUserIdEqualTo(userId);
        List<Long> roleid = userRolesMapper.selectByExample(userRolesExample).stream()
                .map(userrole -> userrole.getRoleId()).collect(Collectors.toList());
        RoleExample example = new RoleExample();
        example.createCriteria().andIdIn(roleid);
        return new HashSet<Role>(roleMapper.selectByExample(example));
    }

    public Role setDefaultRoleByuserId(Long userId) {
        UserRoles userrole = new UserRoles();
        userrole.setUserId(userId);
        userrole.setRoleId(getDefaultRole().getId());
        userRolesMapper.insert(userrole);
        return getDefaultRole();
    }

    public void deleteByuserid(Long id) {
        UserRolesExample example = new UserRolesExample();
        example.createCriteria().andUserIdEqualTo(id);
        userRolesMapper.deleteByExample(example);
    }

    private Role getDefaultRole() {
        RoleExample example = new RoleExample();
        example.createCriteria().andIsDefaultEqualTo(true);
        return roleMapper.selectByExample(example).get(0);
    }

}